package com.aaron.Caitlyn.system.service.impl;

import java.util.List;

import org.Caitlyn.common.util.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aaron.Caitlyn.system.dao.BdDataDictionaryMapper;
import com.aaron.Caitlyn.system.pojo.BdDataDictionary;
import com.aaron.Caitlyn.system.service.DataDictionaryService;


@Service("dataDictionaryService")
public class DataDictionaryServiceImpl implements DataDictionaryService {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private BdDataDictionaryMapper dataDictionaryMapper;

	@Override
	public List<BdDataDictionary> selectAllDictionary() {
		List<BdDataDictionary> dictionaries = dataDictionaryMapper.selectAllDictionary();
		return dictionaries;
	}

	@Override
	public int insertDictionary(BdDataDictionary dictionary) {
		int result = 0;
		if("".equals(dictionary.getId())||dictionary.getId() != null){
			result = dataDictionaryMapper.updateDictionary(dictionary);
		}else{
			dictionary.setParentPid(dictionary.getPid());
			dictionary.setPid(UUIDUtil.buildUUID());
			result = dataDictionaryMapper.insertDictionary(dictionary);
		}
		return result;
	}

	@Override
	@Transactional
	public int deleteDictionary(String pid) {
		int result = 0;
		//解析字符串按逗号分隔
		String[] id = pid.split(",");
		for(int i=0; i<id.length; i++){
			result = dataDictionaryMapper.deleteDictionary(id[i]);
		}
		return result;
	}
}
